package com.itSession.cn.controller.app;

import com.itSession.cn.entity.CircleInfo;
import com.itSession.cn.mapper.CircleInfoMapper;
import com.itSession.cn.util.FileUtil;
import com.itSession.cn.util.Result;
import com.itSession.cn.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: CircleInfoController
 * @description: CircleInfoController
 * @author: hanP
 * @create: 2022-01-10 10:25:40
 */
@Api(tags = "文章分享模块")
@RequestMapping("/circleInfo")
@RestController
@MapperScan("com.itSession.cn.mapper")
public class CircleInfoController {

    @Autowired
    private CircleInfoMapper circleInfoMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listAllData")
    public Result listAllData() {
    	try {
    		List<CircleInfo> list = circleInfoMapper.listAllData();
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("list")
    public Result listAll(String jzCircleType) {
        try {
            List<CircleInfo> list = circleInfoMapper.listAll(jzCircleType);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByAccount")
    public Result listByAccount(String jzCircleAccount) {
        try {
            List<CircleInfo> list = circleInfoMapper.listByAccount(jzCircleAccount);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByClassName")
    public Result listByClassName(String className,String jzCircleType) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("jzCircleType",jzCircleType);
            map.put("className",className);
            List<CircleInfo> list = circleInfoMapper.listByClassName(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByMajorName")
    public Result listByMajorName(String majorName,String jzCircleType) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("jzCircleType",jzCircleType);
            map.put("majorName",majorName);
            List<CircleInfo> list = circleInfoMapper.listByMajorName(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }


    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByJzCircleType")
    public Result listByJzCircleType(String jzCircleType,String jzCircleState) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("jzCircleType",jzCircleType);
            map.put("jzCircleState",jzCircleState);
            List<CircleInfo> list = circleInfoMapper.listByJzCircleType(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    
     /**
     * 分页查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listByParam")
    public Result listByParam(String keyword) {
    	try {
         	Map<String, Object> map = new HashMap<>();
            map.put("keyword",keyword);
    		List<CircleInfo> list = circleInfoMapper.listByParam(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }

    @ApiOperation(value = "查询所有记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("listBySearch")
    public Result listBySearch(String jzCircleContent) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("jzCircleContent",jzCircleContent);
            List<CircleInfo> list = circleInfoMapper.listBySearch(map);
            return ResultGenerator.genSuccessResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }



    /**
     * 根据主键查询
     *
     * @param jzCircleId 主键
     * @return 返回记录，没有返回null
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "加载数据", response = CircleInfo.class)
     @ApiImplicitParams({
            @ApiImplicitParam(name = "jzCircleId", value = "主键", required = false, paramType = "query" ,dataType="int")
    })
    @RequestMapping("getById")
    public Result getById(@ApiIgnore Integer jzCircleId) {
    	try {
    	 	CircleInfo circleInfo = circleInfoMapper.getById(jzCircleId);
         	return ResultGenerator.genSuccessResult(circleInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500, e.getMessage());
        }
       
    }
    
    
    /**
     * 新增，忽略null字段
     *
     * @param circleInfo 新增的记录
     * @return 返回影响行数
     */
      @ApiOperation(value = "新增，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "jzCircleId", value = "主键", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "jzCircleAccount", value = "账号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleUsername", value = "用户名", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleLogo", value = "用户头像", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleContent", value = "内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleFile", value = "发布文件内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleTime", value = "发布时间", required = false, paramType = "query" ,dataType="string"),

        })

    @RequestMapping("insertIgnoreNull")
    public Result insertIgnoreNull(@ApiIgnore CircleInfo circleInfo, MultipartFile picture) {
    	try {
    	    if(null != picture){
                String upload = FileUtil.upload(picture);
                circleInfo.setJzCircleFile(upload);
            }
    		circleInfoMapper.insertIgnoreNull(circleInfo);
            return ResultGenerator.genSuccessResult();
       } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
    /**
     * 修改，忽略null字段
     * @param circleInfo 修改的记录
     * @return 返回影响行数
     */
    @ApiOperation(value = "修改，忽略null字段", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @ApiImplicitParams({
                @ApiImplicitParam(name = "jzCircleId", value = "主键", required = false, paramType = "query" ,dataType="int"),
                @ApiImplicitParam(name = "jzCircleAccount", value = "账号", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleUsername", value = "用户名", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleLogo", value = "用户头像", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleContent", value = "内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleFile", value = "发布文件内容", required = false, paramType = "query" ,dataType="string"),
                @ApiImplicitParam(name = "jzCircleTime", value = "发布时间", required = false, paramType = "query" ,dataType="string"),
        })
    @RequestMapping("updateIgnoreNull")
    public Result updateIgnoreNull(@ApiIgnore CircleInfo circleInfo) {
    	 try {
         	 circleInfoMapper.updateIgnoreNull(circleInfo);
            return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
    
     /**
     * 删除记录
     *
     * @param circleInfo 待删除的记录
     */
    @ApiOperation(value = "删除记录", httpMethod = "POST", notes = "加载数据", response = Result.class)
    @RequestMapping("delete")
    public Result delete(@ApiIgnore CircleInfo circleInfo) {
    	 try {
         	 circleInfoMapper.delete(circleInfo);
             return ResultGenerator.genSuccessResult();
         } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genErrorResult(500,e.getMessage());
        }
    }
    
   
    
}